package com.itmuch.usercenter.rocketmq;

import com.itmuch.usercenter.dao.bonus.BonusEventLogMapper;
import com.itmuch.usercenter.dao.user.UserMapper;
import com.itmuch.usercenter.domain.dto.messaing.UserAddBonusMsgDto;
import com.itmuch.usercenter.domain.entity.bonus.BonusEventLog;
import com.itmuch.usercenter.domain.entity.user.User;
import java.util.Date;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author caiqian
 * @date 2021/5/10 9:39
 */
@Service
@RocketMQMessageListener(consumerGroup = "consumer-group",topic = "add-bonus")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@Slf4j
public class AddBonusListener implements RocketMQListener<UserAddBonusMsgDto> {

  private final UserMapper userMapper;
  private final BonusEventLogMapper bonusEventLogMapper;

  @Override
  public void onMessage(UserAddBonusMsgDto userAddBonusMsgDto) {
    //当收到消息的时候执行的业务
    //1 为用户加积分
    Integer userId = userAddBonusMsgDto.getUserId();
    User user = this.userMapper.selectByPrimaryKey(userId);
    Integer bonus = userAddBonusMsgDto.getBonus();
    user.setBonus(user.getBonus()+ bonus);

    this.userMapper.updateByPrimaryKeySelective(user);

    //2 记录日志到bonus_event_log表中
    this.bonusEventLogMapper.insert(BonusEventLog.builder()
        .userId(userId)
        .value(bonus)
        .createTime(new Date())
        .description("投稿加积分...")
        .event("Contribute")
        .build());

    log.info("积分添加完毕!");

  }
}
